package com.rfid.a252robot.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.rfid.a252robot.R;
import com.rfid.a252robot.activity.adapter.BookAdapter;
import com.rfid.a252robot.activity.presenter.BookListPresenter;
import com.rfid.a252robot.activity.view.IBookListView;
import com.rfid.a252robot.base.BaseListActivity;
import com.rfid.a252robot.widget.EditParentLayout;
import com.rfid.a252robot.widget.KeyParentLayout;
import com.rfid.db.bean.Book;
import com.rfid.manager.LogViewManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;


public class BookListActivity extends BaseListActivity<BookAdapter, Book, BookListPresenter> implements TextWatcher, IBookListView {

    protected List<Book> mShowData = new ArrayList<>();
    protected List<Book> mAllData = new ArrayList<>();
    BookListPresenter mBorrowPresenter = new BookListPresenter();

    public static void startActivity(Context context,String reader) {
        Intent intent = new Intent();
        intent.setClass(context,BookListActivity.class);
        intent.putExtra("reader",reader);
        context.startActivity(intent);
    }

    EditText mEditText;
    Bitmap mQrBm;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mEditText = findViewById(R.id.edit);
        mEditText.addTextChangedListener(this);
        mPresenter.getQrBitmap();
        setRightView(R.mipmap.qr_scan, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });
    }

    @Override
    protected int getCountDownTime() {
        return 60000;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAllData.clear();
        mShowData.clear();
        mAllData.addAll(mBorrowPresenter.getBooks());
        mShowData.addAll(mAllData);
        mAdapter.notifyDataSetChanged();
        System.out.println("size = " + mShowData.size());
    }

    private void showPopWindow() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_qr_layout, null);
        PopupWindow pop =new PopupWindow(view, metrics.widthPixels, Util.getScreenHeight(), true);
        pop.setBackgroundDrawable(new ColorDrawable(0x33000000));
        pop.setTouchable(true);
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        uiFlags |= 0x00001000;
        pop.getContentView().setSystemUiVisibility(uiFlags);
        pop.setFocusable(false);
        pop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT,0,0);
        ImageView image = view.findViewById(R.id.qr);
        if (mQrBm != null) {
            image.setImageBitmap(mQrBm);
        }
        Button button = view.findViewById(R.id.input_bt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                showPwdDialog();
            }
        });
        View quit = view.findViewById(R.id.quit);
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setSystemUIVisible(false);
            }
        });
    }

    PopupWindow pop;
    private void showPwdDialog() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_input_layout, null);
        pop =new PopupWindow(view, metrics.widthPixels, Util.getScreenHeight(), true);
        pop.setBackgroundDrawable(new ColorDrawable(0x33000000));
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        uiFlags |= 0x00001000;
        pop.getContentView().setSystemUiVisibility(uiFlags);
        pop.setFocusable(false);
        pop.setTouchable(true);
        pop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT,0,0);
        Button button = view.findViewById(R.id.button);
        View quit = view.findViewById(R.id.quit);
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        KeyParentLayout keyParentLayout = view.findViewById(R.id.keypad);
        EditParentLayout editParentLayout = view.findViewById(R.id.input);
        keyParentLayout.setKeyListener(new KeyParentLayout.KeyListener() {
            @Override
            public void onKey(String key) {
                System.out.println("key = " + key);
                editParentLayout.input(key);
            }

            @Override
            public void onDelete() {
                editParentLayout.delete();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.queryBookByCode(editParentLayout.getInputKeys());
            }
        });
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setSystemUIVisible(false);
            }
        });
    }

    @Override
    public BookListPresenter getPresenter() {
        return new BookListPresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_borrow;
    }

    @Override
    protected BookAdapter getAdapter() {
        return new BookAdapter(mShowData);
    }

    @Override
    protected int getRecyclerViewId() {
        return R.id.recyclerView;
    }

    @Override
    public void onClick(Book box) {
        BookInfoActivity.startActivity(this,box.getBarcode(),getIntent().getStringExtra("reader"));
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        String key = editable.toString();
        if (TextUtils.isEmpty(key)) {
            mShowData.clear();
            mShowData.addAll(mAllData);
            mAdapter.notifyDataSetChanged();
            return;
        }
        mShowData.clear();
        for (Book book:mAllData) {
            String name = book.getName();
            if (name!= null && book.getName().contains(key)) {
                mShowData.add(book);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onBook(Book book) {
        if (book != null) {
            if (pop != null) {
                pop.dismiss();
            }
            BookInfoActivity.startActivity(this,book.getBarcode(),getIntent().getStringExtra("reader"));
        } else {
            Toast.makeText(this,"未查询到验证码对应书籍",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onQrBitmap(Bitmap bm) {
        mQrBm = bm;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
